<?php

namespace App\Console\Commands;

use App\Services\GoodService;
use Carbon\Carbon;
use Illuminate\Console\Command;

class CleanIsbnCommand extends Command {
	/**
	 * The name and signature of the console command.
	 *
	 * @var string
	 */
	protected $signature = 'system:isbn';

	/**
	 * The console command description.
	 *
	 * @var string
	 */
	protected $description = '整理商品isbn';

	/**
	 * Create a new command instance.
	 *
	 * @return void
	 */
	public function __construct() {
		parent::__construct();
	}

	/**
	 * Execute the console command.
	 *
	 * @return mixed
	 */
	public function handle() {
		//获取有isbn的商品
		$date = Carbon::today()->toDateTimeString();
		$goods = GoodService::isbnGoods($date);
		$num = 0;
		foreach ($goods as $key => $good) {
			$title = $good['title'];
			//标题太长的跳过
			if (strlen($title) > 150) {
				continue;
			}
			$notIsbnGoods = GoodService::findGood($title);
			foreach ($notIsbnGoods as $k => $isbnGood) {
				$isbnGood->isbn = $good['isbn'];
				$isbnGood->remark = 1;
				$isbnGood->save();
				$num++;
				print "正在修改第" . $num . "条：item_id:" . $isbnGood->item_id . "isbn:" . $good['isbn'] . "\n";
			}

		}

	}
}
